package book.items.item21;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

public class Note {

	// sort notes by date
	private static class SortByDate implements Comparator<Note>{
		@Override
		public int compare(Note o1, Note o2) {
			if(o1 == null) {
				return  o2 == null ? 0 : 1 ;
			}  
			if(o2 == null) {
				return -1;
			}
			return 	o1.getDate() == null ? 
					o2.getDate() == null ? 0  : 1 :
						o2.getDate() == null ? -1 : 
							o2.getDate().compareTo(o1.getDate());
		}
	}

	public static final Comparator<Note> SORT_BY_DATE = new SortByDate();

	private String description;
	private Date date;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return "(" + getDescription() + ", " + 
		((this.date == null) ? null : dateFormat.format(this.date)) +
		")";
	}
}